var memberUtil=require('../../utils/memberUtil');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    error:1,
    ok:0,
    binddingFlag:false,
    xiugaiFlag:true,
    mobileInfo:[],
    getText:"获取验证码",
    phone:"",
    nickName:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    that.setData({
      userInfo:wx.getStorageSync("loginUserInfo").userInfo==undefined?"":wx.getStorageSync("loginUserInfo").userInfo,
    })
    if(that.data.userInfo.id==undefined) {
      memberUtil.getLogin(app,0,1,that,2);
    }

    var mobile=options.mobile,nickName=options.nickName;
    if(mobile!=null && mobile!="" && mobile!=undefined){
      this.setData({
        binddingFlag:false,
        xiugaiFlag:true,
        mobileInfo:{mobile:mobile,code:""},
        nickName:nickName
      })
    }else{
      that.setData({
        binddingFlag:true,
        xiugaiFlag:false
      })
    }
  },
  bingding:function(){
    this.setData({
      binddingFlag:true,
      xiugaiFlag:false
    })
  },

  // 获取输入手机号
  phoneInput: function(e) {
    var that=this;
    that.setData({
      mobileInfo:{mobile:e.detail.value,code:that.data.mobileInfo.code}
    });
  },
  // 发送验证码
  sendPhone:function(e) {
    var that = this;
    var phone = that.data.mobileInfo.mobile;
    var getText = that.data.getText;
    var n = 59;
    if (getText.indexOf('s重新获得') >= 0) {
      return false;
    }
    if (phone == "" || !(/^1[34578]\d{9}$/.test(phone))) {
      wx.showToast({
        title: '请填写正确的手机号或手机号不能为空',
        icon: 'none',
        duration: 2000
      })
      return false
    } else {
      var time = setInterval(function () {
        var str = '(' + n + ')s' + '重新获得';
        that.setData({
          getText: str
        })
        if (n <= 0) {
          that.setData({
            getText: '重新获得',
            getChange: true
          })
          clearInterval(time);
        }
        n--;
      }, 1000);
      var sendMsgParamsJson={phoneNumber:phone,memberId:that.data.userInfo.id}
      wx.request({
        url:app.globalData.baseUrl+ '/msg/send',
        data:JSON.stringify(sendMsgParamsJson),
        method: 'POST',
        success: function (e) {
          if(e.data.status==200){
            that.setData({
              mobileInfo: {mobile:that.data.mobileInfo.mobile,code:e.data.data.verificationCode}
            })
            wx.showToast({
              title: '验证码成功发送',
              icon: 'success',
              duration: 2000
            })
          }
        }

      })
    }
  },
//开启擦火
  bindSave:function(e){
    var that = this;
    var code = e.detail.value.yzm;
    var password= e.detail.value.mobile;
    if(password==null || password==""){
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 2000
      })
      return false;
    }else if(!(/^1[34578]\d{9}$/.test(password))){
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 2000
      })
      return false;
    }else if(code==null || code=="" || code!=that.data.mobileInfo.code){
      wx.showToast({
        title: '验证码不正确或验证码不能为空',
        icon: 'none',
        duration: 2000
      })
      return false;
    }else{
      wx.redirectTo({
        url: '/pages/my/personalDocument?mobile='+password+"&nickName="+that.data.nickName
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that=this;
    return {
      title: '我的二维码',
      path: "/pages/shop/shareSuccess?parentId=" + that.data.userInfo.id + "&enterType=0",
      imageUrl: app.globalData.loginUserInfo.erweimaUrl
    }
  }
})